Skip to content

IndexError when finding no solutions while using the --intermediate flag #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kristofferNorr opened this issue Apr 9, 2025 · 0 comments

Comments

@kristofferNorr
Copy link

I think I found a bug which occurs when using the flags --intermediate, and not finding any solutions. objective(self) in src/minizinc/result.py picks out the last element in a list inside getattr-function. I think this causes a bug as when we have no solution, solve() in src/minizinc/instance.py returns an empty list which results in IndexError: list index out of range.

Here is the code in result.py, I think a simple if-statement is enough to fix it.

    @property
    def objective(self) -> Optional[Union[int, float]]:
        """Returns objective of the solution

        Returns the objective of the solution when possible. If no solutions
        have been found or the problem did not have an objective, then None is
        returned instead.

        Returns:
            Optional[Union[int, float]]: best objective found or None

        """
        if self.solution is not None:
            if isinstance(self.solution, list):
                return getattr(self.solution[-1], "objective", None) # <----- here is the bug
            else:
                return getattr(self.solution, "objective", None)
        else:
            return None

Please let me know if I have just misunderstood something (and it is not an actual bug).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant